package com.server.modules.sys.scheduled;

import com.server.modules.api.service.IMService;
import com.server.modules.sys.entity.member.Member;
import com.server.modules.sys.entity.supplment.TSupplmentMember;
import com.server.modules.sys.service.member.MemberService;
import com.server.modules.sys.service.supplment.TSupplmentMemberService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 金贤哲 on 2017/3/8 0008.
 */
@Component
public class ScheduleAppRemindTask {
    @Resource
    private MemberService memberService;
    @Resource
    private IMService imService;
    @Resource
    private TSupplmentMemberService tSupplmentMemberService;
    @Scheduled(cron = "0 30 12 ? * 5") //每周五10:30记录

    public void tasksupplement() {

        List<Member> supplements = memberService.supplement();
        for (Member supplement : supplements) {
            TSupplmentMember tSupplmentMember = new TSupplmentMember();
            tSupplmentMember.setMemberId(supplement.getId());
            tSupplmentMember.setSupplmentTime(new Date());
            tSupplmentMemberService.save(tSupplmentMember);
        }
    }
    @Scheduled(cron = "0 35 19 15 * ?") //每月15日19:35推送消息
    public void sendmessages(){
        //1.查询需要发送消息的客户
        System.out.println("Hello Word!" + new Date());
        List<TSupplmentMember> sendmessages = tSupplmentMemberService.sendmessage();
        Map<String, String> msg = new HashedMap();
        msg.put("type", "text");
        msg.put("msg", "您的信息不完善，请尽快完善个人信息！");
        List<String> userList = new ArrayList<String>(1);
        //2.遍历客户，把HxUsername封装在userList
        for (TSupplmentMember tSupplmentMember : sendmessages) {
            userList.add(tSupplmentMember.getHxUsername());
        }
        //3.调接口，发送消息
        imService.sendSysMessages(userList, msg, null);
    }
}
